package com.yangmao.mvc.controller;

import com.github.pagehelper.PageInfo;
import com.yangmao.entity.Admin;
import com.yangmao.entity.WorkPeople;
import com.yangmao.service.api.WorkPeopleService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import util.ResultEntity;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author ‘洋毛’
 * @version 1.0
 * @description: TODO
 * @date 2022/1/5 22:13
 */
@Controller
public class WorkPeopleController {

    @Autowired
    private  WorkPeopleService workPeopleService;

    @GetMapping("/work/people/all.json")
    public ModelAndView getAllWorkPeople(@RequestParam(name="page",required = true,defaultValue = "1") Integer page,
                                         @RequestParam(name="size",required = true,defaultValue = "4")Integer size) {
        ModelAndView mv = new ModelAndView();

        List<WorkPeople> allWorkPeople = workPeopleService.getAllWorkPeople(page,size);

        PageInfo pageInfo = new PageInfo(allWorkPeople);

        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("work_people_list");

        return mv;
    }

    @RequestMapping("/work/people/edit.html")
    public ModelAndView editWorkPeople(@RequestParam("id") String peopleId, HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        WorkPeople people = workPeopleService.getWorkPeopleById(peopleId);
        mv.addObject("people", people);
        mv.setViewName("work_people_edit");

        return mv;
    }

    @PostMapping("/work/people/update.html")
    public String updateWorkPeople(WorkPeople workPeople) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = new Date();
        String str = simpleDateFormat.format(d); //将日期转换为字符串且格式按照之前制定的

        workPeople.setUpdate_time(str);
        workPeopleService.updateWorkPeople(workPeople);
        return "redirect:/work/people/all.json";
    }

    @RequestMapping("/work/people/delete.html")
    public String deleteWorkPeopleById(@RequestParam("id") String id) {
        workPeopleService.deleteWorkPeopleById(id);
        return "redirect:/work/people/all.json";
    }

    @PostMapping("/work/people/add.html")
    public String addWorkPeople(WorkPeople workPeople){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = new Date();
        String str = simpleDateFormat.format(d); //将日期转换为字符串且格式按照之前制定的

        workPeople.setCreate_time(str);
        workPeople.setUpdate_time(str);

        workPeopleService.addWorkPeople(workPeople);

        return "redirect:/work/people/all.json";
    }

    @RequestMapping("/work/people/search.html")
    public ModelAndView searchWorkPeople(@RequestParam("id") String id,
                                         @RequestParam("position") String position,
                                         @RequestParam(name="page",required = true,defaultValue = "1") Integer page,
                                         @RequestParam(name="size",required = true,defaultValue = "4")Integer size){

        ModelAndView mv = new ModelAndView();

        List<WorkPeople> allWorkPeople = workPeopleService.getWorkPeopleByIdAndPosition(id,position,page,size);

        PageInfo pageInfo = new PageInfo(allWorkPeople);

        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("work_people_list");

        return mv;
    }




}
